﻿Imports System.Data.SqlClient
Imports System.Data.SqlServerCe
Imports System.IO

Public Class Login
    Public validar As New Validaciones.Validar
    Public conexion As New SqlCeConnection("Data Source=|DataDirectory|\NuevoZooDB.sdf")
    Public adapter As New SqlCeDataAdapter("Select * from Usuarios", conexion)
    Public midataset As New DataSet
    Friend encargadologin As Boolean
    Friend usuariologin As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim passreal As String

        If TxtUsuario.Text = "" Or TxtPass.Text = "" Then
            MsgBox("Debe introducir un nombre de usuario y una contraseña.", , "Error")
        Else
            conexion.Open()
            Dim comando As New SqlCeCommand("select Password, Encargado from Usuarios where Usuario=@usuario", conexion)
            comando.Parameters.AddWithValue("@usuario", TxtUsuario.Text)
            Dim lector As SqlCeDataReader = comando.ExecuteReader()
            lector.Read()
            Try
                passreal = lector(0)
                If passreal = TxtPass.Text Then
                    Dim ge As New Gestion
                    usuariologin = TxtUsuario.Text
                    'Vamos sumando valores a la progressbar lentamente con sleeps de la clase Thread
                    Do
                        Threading.Thread.Sleep(10)
                        Progbar.PerformStep()
                        If Progbar.Value = 1 Then
                            lblprog.Text = "Cargando formularios..."
                        ElseIf Progbar.Value = 100 Then
                            lblprog.Text = "Cargando interfaz..."
                        ElseIf Progbar.Value = 200 Then
                            lblprog.Text = "Cargando bases de datos..."
                        End If
                        StatusStrip1.Refresh()
                    Loop Until Progbar.Value >= Progbar.Maximum
                    Dim balon As String = "Conectado como" & Chr(13) & "  Usuario:  " & usuariologin.ToUpper
                    NotifyIcon1.BalloonTipText = (balon)
                    NotifyIcon1.ShowBalloonTip(1000)
                    encargadologin = lector(1)
                    TxtPass.Text = ""
                    TxtUsuario.Text = ""
                    Me.Hide()
                    ge.ShowDialog()
                Else
                    MsgBox("Nombre de usuario o contraseña inválidos", , "Aviso")
                    Using writer As New StreamWriter("log.txt", True)
                        writer.WriteLine("Ha habido un intento de conexión fallido con el nombre de usuario " & TxtUsuario.Text & ".  " & DateTime.Now)
                    End Using
                    TxtUsuario.Text = ""
                    TxtPass.Text = ""
                    TxtPass.Focus()
                End If
            Catch ex As Exception
                MsgBox("Nombre de usuario o contraseña inválidos", , "Aviso")
                Using writer As New StreamWriter("log.txt", True)
                    writer.WriteLine("Ha habido un intento de conexión fallido con el nombre de usuario " & TxtUsuario.Text & ".  " & DateTime.Now)
                End Using
                TxtUsuario.Text = ""
                TxtPass.Text = ""
                TxtUsuario.Focus()
            End Try

            conexion.Close()
        End If

    End Sub

    Private Sub Login_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim res As Integer
        res = MsgBox("¿Está seguro de que desea salir del programa?", 20, "Aviso")
        If res <> 6 Then
            e.Cancel = True
        End If
    End Sub


    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
        TxtPass.Text = ""
        TxtUsuario.Text = ""
    End Sub
End Class